package cn.initcap.service.impl;

import cn.initcap.common.ErrorCode;
import cn.initcap.dao.SeckillUserMapper;
import cn.initcap.entity.SeckillUser;
import cn.initcap.exception.GlobalException;
import cn.initcap.redis.RedisService;
import cn.initcap.redis.SeckillUserKey;
import cn.initcap.service.SeckillUserService;
import cn.initcap.util.Md5Util;
import cn.initcap.util.UUIDUtil;
import cn.initcap.vo.LoginVo;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author initcap
 * @date Created in 2018/8/10 PM2:20.
 */
@Service
public class SeckillUserServiceImpl implements SeckillUserService {

    public static final String COOKI_NAME_TOKEN = "token";

    private final SeckillUserMapper seckillUserMapper;
    private final RedisService redisService;

    @Autowired
    public SeckillUserServiceImpl(SeckillUserMapper seckillUserMapper, RedisService redisService) {
        this.seckillUserMapper = seckillUserMapper;
        this.redisService = redisService;
    }

    @Override
    public SeckillUser getById(long id) {
        //取缓存
        SeckillUser user = redisService.get(SeckillUserKey.GET_BY_ID, "" + id, SeckillUser.class);
        if (user != null) {
            return user;
        }
        //取数据库
        user = seckillUserMapper.selectByPrimaryKey(id);
        if (user != null) {
            redisService.set(SeckillUserKey.GET_BY_ID, "" + id, user);
        }
        return user;
    }

    @Override
    public boolean updatePassword(String token, long id, String formPass) {
        //取user
        SeckillUser user = getById(id);
        if (user == null) {
            throw new GlobalException(ErrorCode.MOBILE_NOT_EXIST);
        }
        //更新数据库
        SeckillUser toBeUpdate = new SeckillUser();
        toBeUpdate.setId(id);
        toBeUpdate.setPassword(Md5Util.formPassToDBPass(formPass, user.getSalt()));
        seckillUserMapper.updateByPrimaryKeySelective(toBeUpdate);
        //处理缓存
        redisService.delete(SeckillUserKey.GET_BY_ID, "" + id);
        user.setPassword(toBeUpdate.getPassword());
        redisService.set(SeckillUserKey.TOKEN, token, user);
        return true;
    }

    @Override
    public SeckillUser getByToken(HttpServletResponse response, String token) {
        if (StringUtils.isEmpty(token)) {
            return null;
        }
        SeckillUser user = redisService.get(SeckillUserKey.TOKEN, token, SeckillUser.class);
        //延长有效期
        if (user != null) {
            addCookie(response, token, user);
        }
        return user;
    }

    @Override
    public String login(HttpServletResponse response, LoginVo loginVo) {
        if (loginVo == null) {
            throw new GlobalException(ErrorCode.SYSTEM_ERROR);
        }
        String mobile = loginVo.getMobile();
        String formPass = loginVo.getPassword();
        //判断手机号是否存在
        SeckillUser user = getById(Long.parseLong(mobile));
        if (user == null) {
            throw new GlobalException(ErrorCode.MOBILE_NOT_EXIST);
        }
        //验证密码
        String dbPass = user.getPassword();
        String saltDB = user.getSalt();
        String calcPass = Md5Util.formPassToDBPass(formPass, saltDB);
        if (!calcPass.equals(dbPass)) {
            throw new GlobalException(ErrorCode.PASSWORD_ERROR);
        }
        //生成cookie
        String token = UUIDUtil.uuid();
        addCookie(response, token, user);
        return token;
    }

    private void addCookie(HttpServletResponse response, String token, SeckillUser user) {
        redisService.set(SeckillUserKey.TOKEN, token, user);
        Cookie cookie = new Cookie(COOKI_NAME_TOKEN, token);
        cookie.setMaxAge(SeckillUserKey.TOKEN.expireSeconds());
        cookie.setPath("/");
        response.addCookie(cookie);
    }
}
